class Solution
{
public:
    string customSortString(string order, string s)
    {
        unordered_map<char, int> orderMap;
        int id = 0;
        for (char c : order)
        {
            orderMap[c] = id++;
        }

        auto compare = [&orderMap](char c1, char c2)
        {
            if (orderMap.count(c1))
            {
                if (orderMap.count(c1))
                {
                    return orderMap[c1] < orderMap[c2];
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (orderMap.count(c2))
                {
                    return true;
                }
                else
                {
                    return c1 < c2;
                }
            }
        };
        sort(s.begin(), s.end(), compare);
        return s;
    }
};